<template>
    <div class="xuanKe-container">
      <el-card class="full-table" shadow="hover" style="margin-top: 8px">
        <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
        <!-- <el-table-column prop="xuankeTaskId" label="选课任务" width="200" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.xuankeTaskIdName}}</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="name" label="课程名称" width="200" show-overflow-tooltip="" />

        <el-table-column label="操作" width="130" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('listByTeacherId:print') || auth('listByTeacherId:dianming')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openDianming(scope.row)" v-auth="'listByTeacherId:dianming'"> 点名 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openChengji(scope.row)" v-auth="'listByTeacherId:dianming'"> 成绩 </el-button>
            <br />
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openPrintMingdan(scope.row)" v-auth="'listByTeacherId:print'"> 打印 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openPrintQiandaoHuizong(scope.row)" v-auth="'listByTeacherId:print'"> 汇总 </el-button>
          </template>
        </el-table-column>
      </el-table>
        <el-pagination
                  v-model:currentPage="tableParams.page"
                  v-model:page-size="tableParams.pageSize"
                  :total="tableParams.total"
                  :page-sizes="[10, 20, 50, 100, 200, 500]"
                  small=""
                  background=""
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  layout="total, sizes, prev, pager, next, jumper"
      />
      <printDialog
        ref="printDialogRef"
        :title="printMingdanTitle"
        @reloadTable="handleQuery" />
      <dianmingDialog
          ref="dianmingDialogRef"
          :title="dianmingTitle"
          @reloadTable="handleQuery"
        />
        <chengjiDialog
          ref="chengjiDialogRef"
          :title="chengjiTitle"
          @reloadTable="handleQuery"
        />
        
      </el-card>
    </div>
  </template>
  
  <script lang="ts" setup="" name="xuanKe">
    import { ref } from "vue";
    import { ElMessageBox, ElMessage } from "element-plus";
    import { auth } from '/@/utils/authFunction';
    import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
    import { formatDate } from '/@/utils/formatTime';
  
    import dianmingDialog from '/@/views/main/myxuanke/component/dianmingDialog.vue'
    import chengjiDialog from '/@/views/main/myxuanke/component/chengjiDialog.vue'
    
    import { listByTeacherId } from '/@/api/main/kecheng';
    import { pageListByTeacherId } from '/@/api/main/myXuanke';
    import { getAPI } from '/@/utils/axios-utils';

    import { hiprint } from 'vue-plugin-hiprint';
    import { SysPrintApi } from '/@/api-services/api';
    import { SysPrint } from '/@/api-services/models';
    import printDialog from '/@/views/system/print/component/hiprint/preview.vue'

    const showAdvanceQueryUI = ref(false);
    const printDialogRef = ref();
    const dianmingDialogRef = ref();
    const chengjiDialogRef = ref();
    const loading = ref(false);
    const tableData = ref<any>([]);
    const queryParams = ref<any>({});
    const tableParams = ref({
      page: 1,
      pageSize: 10,
      total: 0,
    });
  
    const dianmingTitle = ref("");
    const chengjiTitle = ref("");
    const printMingdanTitle = ref("");

      // 打开编辑页面
    const openDianming = (row: any) => {
        dianmingTitle.value = '我要点名';
        dianmingDialogRef.value.openDialog(row);
    };
    const openChengji = (row: any) => {
        chengjiTitle.value = '输入成绩';
        chengjiDialogRef.value.openDialog(row);
    };
    
    // 查询操作
    const handleQuery = async () => {
      loading.value = true;
      var res = await listByTeacherId();
      tableData.value = res.data.result ?? [];
      //tableParams.value.total = res.data.result?.total;
      loading.value = false;
    };
  
    // 改变页面容量
    const handleSizeChange = (val: number) => {
      tableParams.value.pageSize = val;
      handleQuery();
    };
  
    // 改变页码序号
    const handleCurrentChange = (val: number) => {
      tableParams.value.page = val;
      handleQuery();
    };
    
    handleQuery();

      // 打开打印页面
    const openPrintMingdan = async (row: any) => {
        printMingdanTitle.value = '打印社团学生名单';
        var res = await getAPI(SysPrintApi).apiSysPrintPrintNameGet('社团学生名单');
        var printTemplate = res.data.result as SysPrint;
        var template = JSON.parse(printTemplate.template);
        let printData = JSON.parse(JSON.stringify([{'id': row.id}]));
        
        for(var i=0;i<printData.length;i++){
            printData[i]['title'] = row.xuankeTaskIdName + ' '+ row.name;
            printData[i]['printDate'] = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS');
            var studentRes = await pageListByTeacherId({kechengId:printData[i].id,pageSize:1000})
            printData[i]['table'] = studentRes.data.result?.items ?? [];
        }
        console.log(printData);
        printDialogRef.value.showDialog(new hiprint.PrintTemplate({template: template}), printData,'社团学生名单',template.panels[0].width);
    }

    const openPrintQiandaoHuizong = async (row: any) => {
        printMingdanTitle.value = '打印社团学生签到汇总';
        var res = await getAPI(SysPrintApi).apiSysPrintPrintNameGet('社团学生签到汇总');
        var printTemplate = res.data.result as SysPrint;
        var template = JSON.parse(printTemplate.template);
        let printData = JSON.parse(JSON.stringify([{'id': row.id}]));
        
        for(var i=0;i<printData.length;i++){
            printData[i]['title'] = row.xuankeTaskIdName + ' '+ row.name;
            printData[i]['printDate'] = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS');
            var studentRes = await pageListByTeacherId({kechengId:printData[i].id,pageSize:1000})
            printData[i]['table'] = studentRes.data.result?.items ?? [];
        }
        console.log(printData);
        printDialogRef.value.showDialog(new hiprint.PrintTemplate({template: template}), printData,'社团学生签到汇总',template.panels[0].width);
    }
  </script>
  <style scoped>
  :deep(.el-ipnut),
  :deep(.el-select),
  :deep(.el-input-number) {
      width: 100%;
  }
  </style>
  
  